package com.central.modules.job.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.constant.Constant;
import com.central.modules.job.dao.ScheduleJobDao;
import com.central.modules.job.entity.ScheduleJobEntity;
import com.central.modules.job.model.ScheduleJobQueryBean;
import com.central.modules.job.service.ScheduleJobService;
import com.central.modules.job.utils.ScheduleUtils;
import lombok.AllArgsConstructor;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
@AllArgsConstructor
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
    private final Scheduler scheduler;

    @Override
    public IPage<ScheduleJobEntity> pageSearch(ScheduleJobQueryBean queryBean) {
        Page<ScheduleJobEntity> page = new Page<>(queryBean.getPageStart(), queryBean.getPageSize());
        LambdaQueryWrapper<ScheduleJobEntity> queryWrapper = new LambdaQueryWrapper<ScheduleJobEntity>();
        queryWrapper.orderByAsc(ScheduleJobEntity::getCreateDate);
        queryWrapper.like(StrUtil.isNotBlank(queryBean.getBeanName()), ScheduleJobEntity::getBeanName, queryBean.getBeanName());
        return this.page(page, queryWrapper);
    }

    @Override
    public ScheduleJobEntity getEntityById(Long id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ScheduleJobEntity entity) {
        entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        this.save(entity);
        ScheduleUtils.createScheduleJob(scheduler, entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ScheduleJobEntity entity) {
        ScheduleUtils.updateScheduleJob(scheduler, entity);
        this.updateById(entity);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            ScheduleUtils.deleteScheduleJob(scheduler, id);
        }
        //删除数据
        this.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    public boolean updateBatch(Long[] ids, int status) {
        UpdateWrapper<ScheduleJobEntity> updateWrapper = new UpdateWrapper<ScheduleJobEntity>();
        updateWrapper.lambda()
                .in(ScheduleJobEntity::getId, ids)
                .set(ScheduleJobEntity::getStatus, status)
        ;
        return this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(Long[] ids) {
        for (Long id : ids) {
            ScheduleUtils.run(scheduler, this.getById(id));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pause(Long[] ids) {
        for (Long id : ids) {
            ScheduleUtils.pauseJob(scheduler, id);
        }
        updateBatch(ids, Constant.ScheduleStatus.PAUSE.getValue());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resume(Long[] ids) {
        for (Long id : ids) {
            ScheduleUtils.resumeJob(scheduler, id);
        }
        updateBatch(ids, Constant.ScheduleStatus.NORMAL.getValue());
    }

}